package service

import (
	"github.com/vueadmin/app/api/internal/dto"
	"github.com/vueadmin/app/api/internal/model"
	"github.com/vueadmin/app/api/internal/svc"
	"github.com/vueadmin/app/api/internal/vo"
	"github.com/vueadmin/utils/conv"
)

type linkService struct{
	svcCtx *svc.ServiceContext
}

func NewLinkService(svcCtx *svc.ServiceContext) *linkService {
	return &linkService{
		svcCtx: svcCtx,
	}
}

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func (p linkService) GetPageList(req *dto.LinkPageReq) ([]*vo.LinkList, int64, error) {
	var (
		entity model.Link
		list  []*vo.LinkList
		count int64
	)
	query := p.svcCtx.Db.Model(&entity)
	if !conv.IsEmpty(req.LinkId) {
		query = query.Where("link_id = ?", req.LinkId)
	}
	if !conv.IsEmpty(req.Title) {
		query = query.Where("title = ?", req.Title)
	}
	if !conv.IsEmpty(req.LinkcataId) {
		query = query.Where("linkcata_id = ?", req.LinkcataId)
	}
	if !conv.IsEmpty(req.Status) {
		query = query.Where("status = ?", req.Status)
	}
	if !conv.IsEmpty(req.CreateTime){
		query = query.Where("create_time BETWEEN ? AND ?", conv.UnixTime(req.CreateTime[0]), conv.UnixTime(req.CreateTime[1]))
	}
	orderBy := "sortid asc"
	query.Count(&count)
	if err := query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit).Order(orderBy).Find(&list).Error; err != nil {
		return nil, 0, err
	}
	return list, count, nil
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func (p linkService) Add(req *dto.LinkAddReq) (int, error) {
	entity := model.Link{}
	entity.Title = req.Title
	entity.Url = req.Url
	entity.LinkcataId = req.LinkcataId
	entity.Logo = req.Logo
	entity.Status = req.Status
	entity.Sortid = req.Sortid
	entity.CreateTime = conv.NowUnix()

	if err := p.svcCtx.Db.Create(&entity).Error; err != nil {
		return 0, err
	}
	if entity.Sortid == 0 {
		if err := p.svcCtx.Db.Model(&entity).Update("sortid", entity.LinkId).Error; err != nil {
			return 0, err
		}
	}
	return entity.LinkId, nil
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func (p linkService) Update(req *dto.LinkUpdateReq) error {
	entity := model.Link{
		LinkId: req.LinkId,
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	entity.Title = req.Title
	entity.Url = req.Url
	entity.LinkcataId = req.LinkcataId
	entity.Logo = req.Logo
	entity.Status = req.Status
	entity.Sortid = req.Sortid
	entity.CreateTime = conv.UnixTime(req.CreateTime)

	field := conv.StringToSlice("title,url,linkcata_id,logo,status,sortid,create_time", ",")

	if err := query.Select(field).Updates(&entity).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func (p linkService) Delete(req *dto.LinkDeleteReq) error {
	query := p.svcCtx.Db
	query = query.Where("link_id IN ?", req.LinkId)
	if err := query.Delete(&model.Link{}).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func (p linkService) Detail(req *dto.LinkDetailReq) (*vo.LinkDetail, error) {
	var info vo.LinkDetail
	query := p.svcCtx.Db.Model(&model.Link{})
	if err := query.Where("link_id = ?",req.LinkId).Limit(1).First(&info).Error; err != nil {
		return nil, err
	}
	return &info, nil
}

